package com.rc.saas.tenant.service.member;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.mapper.member.RcMemberGainSettingMapper;
import com.rc.saas.tenant.model.member.RcMemberGainGradeSetting;
import com.rc.saas.tenant.model.member.RcMemberGainGradeSettingExample;
import com.rc.saas.tenant.model.member.RcMemberGainSetting;
import com.rc.saas.tenant.model.member.RcMemberGainSettingExample;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RcMemberGainSettingServiceImpl implements RcMemberGainSettingService {

    @Autowired
    private RcMemberGainSettingMapper memberGainSettingMapper;

    @Autowired
    private RcMemberGainGradeSettingService memberGainGradeSettingService;


    @Override
    public RcMemberGainSettingMapper getMapper() {
        return memberGainSettingMapper;
    }

    @Override
    public PageInfo listPageMemberGain(String gainName,String sublevel, Integer pageNo, Integer pageSize) {
        List<RcMemberGainSetting> list = memberGainSettingMapper.listMemberGain(gainName, sublevel);
        return new PageInfo(list);
    }

    @Override
    public void addMemberGainSetting(RcMemberGainSetting memberGainSetting, List<RcMemberGainGradeSetting> grades) {
        memberGainSettingMapper.insertReturnId(memberGainSetting);
        grades.forEach(grade -> {
            grade.setMemberGainId(memberGainSetting.getMemberGainId());
        });
        memberGainGradeSettingService.batchInsert(grades);
    }

    @Override
    public void updateMemberGainSetting(RcMemberGainSetting memberGainSetting,
                                        List<RcMemberGainGradeSetting> addGradeList,
                                        List<RcMemberGainGradeSetting> editGradeList,
                                        List<RcMemberGainGradeSetting> delGradeList) {
        memberGainSettingMapper.updateByPrimaryKeySelective(memberGainSetting);
        if (!CollectionUtils.isEmpty(addGradeList)) {
            addGradeList.forEach(grade -> {
                grade.setMemberGainId(memberGainSetting.getMemberGainId());
            });
            memberGainGradeSettingService.batchInsert(addGradeList);
        }
        if (!CollectionUtils.isEmpty(editGradeList)) {
            memberGainGradeSettingService.batchUpdate(editGradeList);
        }
        if (!CollectionUtils.isEmpty(delGradeList)) {
            memberGainGradeSettingService.batchDelete(delGradeList);
        }




    }

    @Override
    @Transactional
    public int editGain(RcMemberGainSetting memberGainSetting) {
        memberGainSettingMapper.updateByPrimaryKey(memberGainSetting);
        RcMemberGainGradeSettingExample childExample = new RcMemberGainGradeSettingExample();
        childExample.createCriteria().andMemberGainIdEqualTo(memberGainSetting.getMemberGainId());
        //先删
        memberGainGradeSettingService.deleteByExample(childExample);
        if ((StringUtils.isNotBlank(memberGainSetting.getIsDefault())
                &&StringUtils.equals("1",memberGainSetting.getIsDefault()))&&(StringUtils.isNotBlank(memberGainSetting.getStatus())
                &&StringUtils.equals("0",memberGainSetting.getStatus()))){
           /* //同一等级下只有一个默认*/
            RcMemberGainSetting updateStatusInfo = new RcMemberGainSetting();
            updateStatusInfo.setIsDefault("0");
            updateStatusInfo.setStatus("1");
            memberGainSettingMapper.updateByPrimaryKey(updateStatusInfo);
        }else if((StringUtils.isNotBlank(memberGainSetting.getIsDefault())
                &&StringUtils.equals("0",memberGainSetting.getIsDefault()))&&(StringUtils.isNotBlank(memberGainSetting.getStatus())
                &&StringUtils.equals("0",memberGainSetting.getStatus()))){
            RcMemberGainSetting updateStatusInfo = new RcMemberGainSetting();
            updateStatusInfo.setStatus("1");
            memberGainSettingMapper.updateByPrimaryKey(updateStatusInfo);
        }
        List<RcMemberGainGradeSetting> settings = null;
        if(StringUtils.equals("0",memberGainSetting.getGainType())){
            settings = new ArrayList<>();
            JSONArray jsonArray = JSONArray.parseArray(memberGainSetting.getChargeRebateStr());
            for (int i = 0; i <jsonArray.size() ; i++) {
                JSONObject temp = jsonArray.getJSONObject(i);
                RcMemberGainGradeSetting setting = new RcMemberGainGradeSetting();
                setting.setMemberGainId(memberGainSetting.getMemberGainId());
                setting.setSettingType("0");
                setting.setSettingSort(temp.getInteger("id"));
                setting.setRechargeAmount(temp.getBigDecimal("rechargeAmount"));
                setting.setDiscountAmount(temp.getBigDecimal("discountAmount"));
                setting.setCreatedTime(new Date());
                settings.add(setting);
            }
            memberGainGradeSettingService.batchInsert(settings);
        }else if (StringUtils.equals("1",memberGainSetting.getGainType())){
            settings = new ArrayList<>();
            JSONArray jsonArray = JSONArray.parseArray(memberGainSetting.getChargeHandselStr());
            for (int i = 0; i <jsonArray.size() ; i++) {
                JSONObject temp = jsonArray.getJSONObject(i);
                RcMemberGainGradeSetting setting = new RcMemberGainGradeSetting();
                setting.setMemberGainId(memberGainSetting.getMemberGainId());
                setting.setSettingType("1");
                setting.setSettingSort(temp.getInteger("id"));
                setting.setRechargeAmount(temp.getBigDecimal("rechargeAmount"));
                setting.setGainAmount(temp.getBigDecimal("gainAmount"));
                setting.setConsumeUseAmount(temp.getBigDecimal("consumeUseAmount"));
                setting.setCreatedTime(new Date());
                settings.add(setting);
            }
            memberGainGradeSettingService.batchInsert(settings);
        }else {
            JSONObject json = JSONObject.parseObject(memberGainSetting.getBirthdayStr());
            if (StringUtils.isNotBlank(memberGainSetting.getConsumeEnum())&&StringUtils.equals("1",memberGainSetting.getConsumeEnum())){
                settings = new ArrayList<>();
                JSONArray jsonArray = json.getJSONArray("rebateTable");
                for (int i = 0; i <jsonArray.size() ; i++) {
                    JSONObject temp = jsonArray.getJSONObject(i);
                    RcMemberGainGradeSetting setting = new RcMemberGainGradeSetting();
                    setting.setMemberGainId(memberGainSetting.getMemberGainId());
                    setting.setSettingType("0");
                    setting.setSettingSort(temp.getInteger("id"));
                    setting.setRechargeAmount(temp.getBigDecimal("rechargeAmount"));
                    setting.setDiscountAmount(temp.getBigDecimal("discountAmount"));
                    setting.setCreatedTime(new Date());
                    settings.add(setting);
                }
                memberGainGradeSettingService.batchInsert(settings);
            }
            if(StringUtils.isNotBlank(memberGainSetting.getHandselEnum())&&StringUtils.equals("1",memberGainSetting.getHandselEnum())){
                settings = new ArrayList<>();
                JSONArray jsonArray = json.getJSONArray("handselTable");
                for (int i = 0; i <jsonArray.size() ; i++) {
                    JSONObject temp = jsonArray.getJSONObject(i);
                    RcMemberGainGradeSetting setting = new RcMemberGainGradeSetting();
                    setting.setMemberGainId(memberGainSetting.getMemberGainId());
                    setting.setSettingType("1");
                    setting.setSettingSort(temp.getInteger("id"));
                    setting.setRechargeAmount(temp.getBigDecimal("rechargeAmount"));
                    setting.setGainAmount(temp.getBigDecimal("gainAmount"));
                    setting.setConsumeUseAmount(temp.getBigDecimal("consumeUseAmount"));
                    setting.setCreatedTime(new Date());
                    settings.add(setting);
                }
                memberGainGradeSettingService.batchInsert(settings);
            }
        }
        return 1;
    }

    @Override
    @Transactional
    public int saveGain(RcMemberGainSetting memberGainSetting) {
        if ((StringUtils.isNotBlank(memberGainSetting.getIsDefault())
                &&StringUtils.equals("1",memberGainSetting.getIsDefault()))&&(StringUtils.isNotBlank(memberGainSetting.getStatus())
                &&StringUtils.equals("0",memberGainSetting.getStatus()))){
           /* //同一等级下只有一个默认
            RcMemberGainSettingExample example = new RcMemberGainSettingExample();
            example.createCriteria().andMemberLevelEqualTo(memberGainSetting.getMemberLevel())
                    .andMemberLevelEqualTo(memberGainSetting.getMemberLevel()).andIsDefaultEqualTo("1");
            List<RcMemberGainSetting> lstMemberSettings = memberGainSettingMapper.selectByExample(example);
            if (lstMemberSettings.size()>0){
                return 0;
            }*/
            RcMemberGainSetting updateStatusInfo = new RcMemberGainSetting();
            updateStatusInfo.setIsDefault("0");
            updateStatusInfo.setStatus("1");
            memberGainSettingMapper.updateByPrimaryKey(updateStatusInfo);
        }else if((StringUtils.isNotBlank(memberGainSetting.getIsDefault())
                &&StringUtils.equals("0",memberGainSetting.getIsDefault()))&&(StringUtils.isNotBlank(memberGainSetting.getStatus())
                &&StringUtils.equals("0",memberGainSetting.getStatus()))){
            RcMemberGainSetting updateStatusInfo = new RcMemberGainSetting();
            updateStatusInfo.setStatus("1");
            memberGainSettingMapper.updateByPrimaryKey(updateStatusInfo);
        }
        memberGainSettingMapper.insertReturnId(memberGainSetting);
        List<RcMemberGainGradeSetting> settings = null;
        if(StringUtils.equals("0",memberGainSetting.getGainType())){
            settings = new ArrayList<>();
            JSONArray jsonArray = JSONArray.parseArray(memberGainSetting.getChargeRebateStr());
            for (int i = 0; i <jsonArray.size() ; i++) {
                JSONObject temp = jsonArray.getJSONObject(i);
                RcMemberGainGradeSetting setting = new RcMemberGainGradeSetting();
                setting.setMemberGainId(memberGainSetting.getMemberGainId());
                setting.setSettingType("0");
                setting.setSettingSort(temp.getInteger("id"));
                setting.setRechargeAmount(temp.getBigDecimal("rechargeAmount"));
                setting.setDiscountAmount(temp.getBigDecimal("discountAmount"));
                setting.setCreatedTime(new Date());
                settings.add(setting);
            }
            memberGainGradeSettingService.batchInsert(settings);
        }else if (StringUtils.equals("1",memberGainSetting.getGainType())){
            settings = new ArrayList<>();
            JSONArray jsonArray = JSONArray.parseArray(memberGainSetting.getChargeHandselStr());
            for (int i = 0; i <jsonArray.size() ; i++) {
                JSONObject temp = jsonArray.getJSONObject(i);
                RcMemberGainGradeSetting setting = new RcMemberGainGradeSetting();
                setting.setMemberGainId(memberGainSetting.getMemberGainId());
                setting.setSettingType("1");
                setting.setSettingSort(temp.getInteger("id"));
                setting.setRechargeAmount(temp.getBigDecimal("rechargeAmount"));
                setting.setGainAmount(temp.getBigDecimal("gainAmount"));
                setting.setConsumeUseAmount(temp.getBigDecimal("consumeUseAmount"));
                setting.setCreatedTime(new Date());
                settings.add(setting);
            }
            memberGainGradeSettingService.batchInsert(settings);
        }else {
            JSONObject json = JSONObject.parseObject(memberGainSetting.getBirthdayStr());
            if (StringUtils.isNotBlank(memberGainSetting.getConsumeEnum())&&StringUtils.equals("1",memberGainSetting.getConsumeEnum())){
                settings = new ArrayList<>();
                JSONArray jsonArray = json.getJSONArray("rebateTable");
                for (int i = 0; i <jsonArray.size() ; i++) {
                    JSONObject temp = jsonArray.getJSONObject(i);
                    RcMemberGainGradeSetting setting = new RcMemberGainGradeSetting();
                    setting.setMemberGainId(memberGainSetting.getMemberGainId());
                    setting.setSettingType("0");
                    setting.setSettingSort(temp.getInteger("id"));
                    setting.setRechargeAmount(temp.getBigDecimal("rechargeAmount"));
                    setting.setDiscountAmount(temp.getBigDecimal("discountAmount"));
                    setting.setCreatedTime(new Date());
                    settings.add(setting);
                }
                memberGainGradeSettingService.batchInsert(settings);
            }
            if(StringUtils.isNotBlank(memberGainSetting.getHandselEnum())&&StringUtils.equals("1",memberGainSetting.getHandselEnum())){
                settings = new ArrayList<>();
                JSONArray jsonArray = json.getJSONArray("handselTable");
                for (int i = 0; i <jsonArray.size() ; i++) {
                    JSONObject temp = jsonArray.getJSONObject(i);
                    RcMemberGainGradeSetting setting = new RcMemberGainGradeSetting();
                    setting.setMemberGainId(memberGainSetting.getMemberGainId());
                    setting.setSettingType("1");
                    setting.setSettingSort(temp.getInteger("id"));
                    setting.setRechargeAmount(temp.getBigDecimal("rechargeAmount"));
                    setting.setGainAmount(temp.getBigDecimal("gainAmount"));
                    setting.setConsumeUseAmount(temp.getBigDecimal("consumeUseAmount"));
                    setting.setCreatedTime(new Date());
                    settings.add(setting);
                }
                memberGainGradeSettingService.batchInsert(settings);
            }
        }
        return 1;
    }

    @Override
    public List<RcMemberGainSetting> listAvailableMemberGain(String sublevel) {
        return memberGainSettingMapper.listAvailableMemberGain(sublevel);
    }
}
